﻿#pragma once
// Inner Fire 游戏引擎库
// 渲染对象基类
//
// Copyright (c) 2023 by 尔西大帝. All rights reserved.
//
// 渲染对象基类。根据《DirectX12游戏开发实战》中的工具类修改。
// 原作者：Frank Luna
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2024-11-27

#include <dx_core/dx_core.h>

namespace ifire {
class RenderTarget {
public:
  ///< summary>
  /// The width and height should match the dimensions of the input texture to
  /// blur. Recreate when the screen is resized.
  ///</summary>
  RenderTarget(
      ID3D12Device* device, UINT width, UINT height, DXGI_FORMAT format);

  RenderTarget(const RenderTarget& rhs) = delete;
  RenderTarget& operator=(const RenderTarget& rhs) = delete;
  ~RenderTarget() = default;

  ID3D12Resource* Resource();
  CD3DX12_GPU_DESCRIPTOR_HANDLE& Srv();
  CD3DX12_CPU_DESCRIPTOR_HANDLE& Rtv();

  void BuildDescriptors(CD3DX12_CPU_DESCRIPTOR_HANDLE hCpuSrv,
      CD3DX12_GPU_DESCRIPTOR_HANDLE hGpuSrv,
      CD3DX12_CPU_DESCRIPTOR_HANDLE hCpuRtv);

  void OnResize(UINT newWidth, UINT newHeight);
  
  CD3DX12_CPU_DESCRIPTOR_HANDLE mhCpuSrv;
  CD3DX12_GPU_DESCRIPTOR_HANDLE mhGpuSrv;
  CD3DX12_CPU_DESCRIPTOR_HANDLE mhCpuRtv;

  D3D12_CLEAR_VALUE clearValue;
private:
  void BuildDescriptors();
  void BuildResource();

private:
  ID3D12Device* md3dDevice = nullptr;

  UINT mWidth = 0;
  UINT mHeight = 0;
  DXGI_FORMAT mFormat = DXGI_FORMAT_R8G8B8A8_UNORM;


  // Two for ping-ponging the textures.
  Microsoft::WRL::ComPtr<ID3D12Resource> mOffscreenTex = nullptr;
};

} // namespace ifire